package eval.cm.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CollectionUtils {
	
	/**
	 * Adds an item of type V to a list located in a map by a key of type K.
	 * If the list is null then the list is created
	 * 
	 * @param <K> The type of the map key
	 * @param <V> The type of the item in the list
	 * @param map The map
	 * @param key The map key by which the list of V is located
	 * @param value The value to be added
	 */
	public static <K,V> void addItemToMapOfLists(
		final Map<K,List<V>> map,
		final K key,
		final V value
	) {
		List<V> vals = map.get(key);
		if( null == vals ) {
			vals = new ArrayList<V>();
			map.put(key,vals);
		}
		vals.add(value);
	}

}
